package com.hzhk.happyrent.ui.adapter.person;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.rent.ProductInfoModel;
import com.hzhk.happyrent.bean.rent.ProductInfoModel.DataBean.GoodsBean.SpecArrayBean;
import com.hzhk.happyrent.ui.adapter.BaseAdapter;
import com.hzhk.happyrent.ui.adapter.ProductByStagesAdapter;
import com.hzhk.happyrent.ui.view.PredicateLayout;
import com.hzhk.happyrent.utils.ListUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductTypeAdapter extends BaseAdapter<SpecArrayBean> {

    public ProductTypeAdapter(Context context) {
        super(context);
        this.listener = (ButtonSelecterListener) context;
    }


    @Override
    public int getCount() {
        return super.getCount();
    }


    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if ("租期".equals(mList.get(position).getName())) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        int type = getItemViewType(position);
        ViewHolder viewHolder;
        ViewHolderS viewHolderS;
        if (convertView == null) {
            if (type == 0) {
                convertView = mInflater.inflate(R.layout.layout_product_type, null);
                viewHolder = new ViewHolder(convertView, mList.get(position), mInflater, mContext);
                convertView.setTag(viewHolder);
            } else {
                convertView = mInflater.inflate(R.layout.layout_product_rent_type, null);
                viewHolderS = new ViewHolderS(convertView, mList.get(position), mContext);
                convertView.setTag(viewHolderS);
            }
        }
        return convertView;
    }

    ButtonSelecterListener listener;

    class ViewHolder {
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.product_type)
        LinearLayout productType;

        private Context context;

        ViewHolder(View view, final SpecArrayBean specArrayBean, LayoutInflater inflater, final Context context) {
            ButterKnife.bind(this, view);
            this.context = context;
            type.setText(specArrayBean.getName() + "：");
            for (int i = 0; i < specArrayBean.getValue().size(); i++) {
                String s = specArrayBean.getValue().get(i);
                TextView textView = (TextView) inflater.inflate(R.layout.text_product_type, null);
                textView.setText(s);
                if (i == 0) {
                    textView.setSelected(true);
                    textView.setTextColor(context.getResources().getColor(R.color.tab_red));
                    specArrayBean.setSelect(s);
                }
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        setDefaultEnable((LinearLayout) view.getParent());
                        ((TextView) view).setSelected(true);
                        ((TextView) view).setTextColor(context.getResources().getColor(R.color.tab_red));
                        specArrayBean.setSelect(((TextView) view).getText().toString());
                        listener.refresh();
                    }
                });
                productType.addView(textView);
            }
        }

        private void setDefaultEnable(LinearLayout productType) {
            for (int i = 0; i < productType.getChildCount(); i++) {
                ((TextView) productType.getChildAt(i)).setSelected(false);
                ((TextView) productType.getChildAt(i)).setTextColor(context.getResources().getColor(R.color.black_40));
            }
        }
    }

    public interface ButtonSelecterListener {
        void refresh();
    }

    class ViewHolderS {
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.choose_by_stages)
        TextView chooseByStages;
        private Context context;
        private SpecArrayBean specArrayBean;
        PopupWindow mPopupWindow;

        ViewHolderS(View view, final SpecArrayBean specArrayBean, final Context context) {
            ButterKnife.bind(this, view);
            this.context = context;
            this.specArrayBean = specArrayBean;
            type.setText(specArrayBean.getName());
            chooseByStages.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    show(chooseByStages);
                }
            });
        }

        public void show(View v) {
            try {
                if (mPopupWindow == null) {

                    View mView = LayoutInflater.from(context).inflate(R.layout.menu_select_city, null);
                    final ListView city_lv = (ListView) mView.findViewById(R.id.city_lv);
                    city_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> av, View v,
                                                int position, long id) {
                            specArrayBean.setSelect(((ProductByStagesAdapter) (av.getAdapter())).getItem(position));
                            chooseByStages.setTextColor(context.getResources().getColor(R.color.tab_red));
                            chooseByStages.setText(((ProductByStagesAdapter) (av.getAdapter())).getItem(position));
                            listener.refresh();
                            hide();
                        }
                    });
                    ProductByStagesAdapter adapter_test = new ProductByStagesAdapter(context);
                    city_lv.setAdapter(adapter_test);
                    if (adapter_test != null) {
                        adapter_test.setList(specArrayBean.getValue());
                    }
                    mPopupWindow = new PopupWindow(mView,
                            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,
                            true);
                    // mPopupWindow.setAnimationStyle(R.style.popwin_more_anim_style);
                    mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
                    mPopupWindow.setTouchable(true);
                    mPopupWindow.setFocusable(true);
                    mPopupWindow.setSplitTouchEnabled(true);
                    mPopupWindow.setOutsideTouchable(true);
                    mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                                hide();
                                return true;
                            }
                            return false;
                        }
                    });
                }

                mPopupWindow.showAsDropDown(v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void hide() {
            if (mPopupWindow != null) {
                mPopupWindow.dismiss();
                mPopupWindow = null;
            }
            return;
        }
    }
}
